package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.IDishService;
import com.itheima.reggie.service.ISetmealService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 套餐控制层
 * @author panda
 * @since 2023/1/2 18:58
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("新增套餐dto====>{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("套餐保存成功");
    }

    @GetMapping("/page")
    public R<Page> setmealGetPage(Integer page,Integer pageSize,String name) {
        //先做简单版的
        //构建分页对象
        Page pageInfo = new Page(page,pageSize);
        //条件查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && !name.equals(""), Setmeal::getName, name);
        setmealService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }
    //删除
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("套餐id集合===>{}",ids.toString() );
        //因为涉及到两个表的操作,MP处理不了
        //需要新增方法
        setmealService.deleteWithSetmealDish(ids);

        return R.success("删除成功");

    }
    /**
     * 导入代码
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}